#include <unistd.h>
#include <linux/ip.h>
#include <string.h>
#include <time.h>

#include "opendpi_task.h"
#include "log.h"
#include "msg.h"
#include "packet_ops.h"
#include "core_data.h"
#include "protocols.h"
#include "sql.h"

extern void setupDetection(void);
extern unsigned int packet_processing(int *protocol, const struct iphdr *iph, uint16_t ipsize, uint16_t rawsize);

int opendpi_ops(struct msg *msg)
{
	log_info("begin opendpi_ops.");

	return 0;
}

void *opendpi_loop(void *conf)
{
	log_info("begin opendpi_loop.");

	setupDetection();

	packet_init();

	sql_init();

	while (1) {
		void *data = get_packet(PROTOCOL_ALL, NULL);	
		if (data == NULL) {
			sleep(1);
			continue;
		}

		unsigned short ipsize = get_ipsize();
		unsigned short rawsize = get_rawsize();

		int protocol;
		packet_processing(&protocol, data, ipsize, rawsize);
	
		make_tag(protocol, "opendpi");

		free_packet();
	}
}
